﻿using System;
using System.Collections.Generic;
using System.Linq;
using static IronPython.Modules._ast;

namespace CoreLims.Models.CoreLims
{
    public static partial class Eq
    {
        /// <summary>
        /// 获取指定设备
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static EqEquipment getEquipment(CoreLimsContext dbContext, string EqId) =>
            dbContext.Equipment.FirstOrDefault(s => s.Id == EqId);

        /// <summary>
        /// 获取设备关联的扩展信息
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static EqEquipmentInfo getEqInfo(CoreLimsContext dbContext, string EqId) =>
            dbContext.EquipmentInfo.FirstOrDefault(s => s.EquipmentId == EqId);

        /// <summary>
        /// 获取指定设备的所有使用记录
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static List<EqUsageRecord> getUsageRecords(CoreLimsContext dbContext, string EqId) =>
            dbContext.UsageRecord.Where(s => s.EqId == EqId).OrderByDescending(s => s.RecordTime).ToList();

        /// <summary>
        /// 获取指定设备的所有维护记录
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static List<EqMaintain> getMaintains(CoreLimsContext dbContext, string EqId) =>
            dbContext.Maintain.Where(s => s.EqId == EqId).OrderByDescending(s => s.RecordTime).ToList();

        /// <summary>
        /// 获取指定事件的维护记录
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EventId"></param>
        /// <returns></returns>
        public static List<EqMaintain> getMaintainsByEventId(CoreLimsContext dbContext, string EventId) =>
            dbContext.Maintain.Where(s => s.MaintainEventId == EventId).OrderByDescending(s => s.RecordTime).ToList();

        /// <summary>
        /// 获取全部设备列表
        /// </summary>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<EqEquipment> getEquipmentList(CoreLimsContext dbContext) => 
            dbContext.Equipment.OrderBy(s => s.Id).ToList();

        /// <summary>
        /// 根据ID，获得事件
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static EqMaintainEvent getMaintainEvent(CoreLimsContext dbContext, string Id) =>
            dbContext.MaintainEvent.FirstOrDefault(s => s.Id == Id);

        /// <summary>
        /// 获取设备关联的所有维护事件
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static List<EqMaintainEvent> getMaintainEvents(CoreLimsContext dbContext, string EqId) =>
            dbContext.MaintainEvent.Where(s => s.EqId == EqId).OrderBy(s => s.EventsName).ToList();

        /// <summary>
        /// 获取设备及关联信息
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static IQueryable getEquipmentInfo(CoreLimsContext dbContext, string EqId)
        {
            var equipment = from eq in dbContext.Equipment.Where(s => s.Id == EqId)
                            join eqinfo in dbContext.EquipmentInfo on eq.Id equals eqinfo.EquipmentId
                            select new
                            {
                                eq.Id, eq.Name, eq.Type, eq.Manufacturer, eq.Model, eq.Site, eq.Group, eq.SupplierId, eq.EnablingDate,
                                eq.PurchaseDate, eq.FactoryNumber, eq.ResponsiblePersonId, eq.InstallationLocation, eqinfo.EquipmentId
                            };
            return equipment;
        }

        /// <summary>
        /// 新建设备
        /// </summary>
        /// <returns></returns>
        public static string CreatEquipment(CoreLimsContext dbContext, string EqId, string EqName, string EqType, string Site, string Group)
        {
            var temp = getEquipment(dbContext, EqId);
            if (temp != null)
                return "该设备编号已经存在！";
            if (string.IsNullOrEmpty(EqName) && string.IsNullOrEmpty(EqType))
                return "设备名称和类型不能为空！";
            if (string.IsNullOrEmpty(Site) && string.IsNullOrEmpty(Group))
                return "实验室和组不能为空！";

            var eq = new EqEquipment();
            var eqinfo = new EqEquipmentInfo();

            eq.Id = EqId;
            eq.Name = EqName;
            eq.Type = EqType;
            eq.Site = Site;
            eq.Group = Group;
            eqinfo.EquipmentId = EqId;

            dbContext.Equipment.Add(eq);
            dbContext.EquipmentInfo.Add(eqinfo);
            dbContext.SaveChanges();
            return "设备添加成功！";
        }

        /// <summary>
        /// 新建设备维护事件
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <param name="EventsName"></param>
        /// <param name="Description"></param>
        /// <returns></returns>
        public static string CreatMaintainEvent(CoreLimsContext dbContext, string EqId, string EventsName, string Description, string SendToPerson, string SendToPersonId)
        {
            var me = new EqMaintainEvent();

            me.EqId = EqId;
            me.EventsName = EventsName;
            me.Description = Description;
            me.SendToPerson = SendToPerson;
            me.SendToPersonId = SendToPersonId;

            dbContext.MaintainEvent.Add(me);
            dbContext.SaveChanges();
            return "设备维护事件添加成功！";
        }

        /// <summary>
        /// 更新设备状态 启用/停用
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static string UpdateEquipmentStatus(CoreLimsContext dbContext, string EqId)
        {
            var eq = getEquipment(dbContext, EqId);

            if (eq.Status == "启用")
                eq.Status = "停用";
            else
                eq.Status = "启用";

            dbContext.Equipment.Update(eq);
            dbContext.SaveChanges();
            return "状态更新成功！";
        }

        /// <summary>
        /// 更新设备维护事件状态 启用/停用
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static string UpdateMaintainEventStatus(CoreLimsContext dbContext, string Id)
        {
            var me = getMaintainEvent(dbContext, Id);

            if (me.Status == "启用")
                me.Status = "停用";
            else
                me.Status = "启用";

            dbContext.MaintainEvent.Update(me);
            dbContext.SaveChanges();
            return "状态更新成功！";
        }

        /// <summary>
        /// 更新设备维护事件中的上次维护完成时间
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="newTime"></param>
        /// <returns></returns>
        public static string UpdateMaintainEventLastTime(CoreLimsContext dbContext, string Id, string newTime)
        {
            var me = Eq.getMaintainEvent(dbContext, Id);
            me.LastTime = DateTime.Parse(newTime);

            dbContext.MaintainEvent.Update(me);
            dbContext.SaveChanges();

            if (me.NextTime is null)
                return "由于缺少频率或频率单位，未能计算出“下次维护时间”";
            else
                return "更新成功！";
        }

        /// <summary>
        /// 更新设备维护事件中的频率
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="Frequency"></param>
        /// <returns></returns>
        public static string UpdateMaintainEventFrequency(CoreLimsContext dbContext, string Id, string Frequency)
        {
            var me = Eq.getMaintainEvent(dbContext, Id);
            me.Frequency = int.Parse(Frequency);

            dbContext.MaintainEvent.Update(me);
            dbContext.SaveChanges();

            if (me.NextTime is null)
                return "由于缺少频率或频率单位，未能计算出“下次维护时间”";
            else
                return "频率更新成功！";
        }

        /// <summary>
        /// 更新设备维护事件中的频率单位
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="FrequencyUnit"></param>
        /// <returns></returns>
        public static string UpdateMaintainEventFrequencyUnit(CoreLimsContext dbContext, string Id, string FrequencyUnit)
        {
            var me = Eq.getMaintainEvent(dbContext, Id);
            me.FrequencyUnit = FrequencyUnit;

            dbContext.MaintainEvent.Update(me);
            dbContext.SaveChanges();

            if (me.NextTime is null)
                return "由于缺少频率或频率单位，未能计算出“下次维护时间”";
            else
                return "频率单位更新成功！";
        }

        /// <summary>
        /// 更新设备维护事件中的提醒天数
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="Remind"></param>
        /// <returns></returns>
        public static string UpdateMaintainEventRemind(CoreLimsContext dbContext, string Id, string Remind)
        {
            var me = Eq.getMaintainEvent(dbContext, Id);
            me.Remind = int.Parse(Remind);

            dbContext.MaintainEvent.Update(me);
            dbContext.SaveChanges();
            return "提醒更新成功！";
        }

        /// <summary>
        /// 更新设备维护事件中的提醒组
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <param name="SendToGroup"></param>
        /// <param name="SendToGroupId"></param>
        /// <returns></returns>
        public static string UpdateMaintainGroup(CoreLimsContext dbContext, string Id, string SendToGroup, string SendToGroupId)
        {
            var me = Eq.getMaintainEvent(dbContext, Id);
            me.SendToGroup = SendToGroup;
            me.SendToGroupId = SendToGroupId;

            dbContext.MaintainEvent.Update(me);
            dbContext.SaveChanges();
            return "提醒组更新成功！";
        }

        /// <summary>
        /// 报废设备
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static string UpdateEquipmentScrap(CoreLimsContext dbContext, string EqId)
        {
            var eq = getEquipment(dbContext, EqId);
            eq.Status = "报废";

            dbContext.Equipment.Update(eq);
            dbContext.SaveChanges();
            return "状态更新成功！";
        }

        /// <summary>
        /// 删除设备及设备关联的所有信息
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="EqId"></param>
        /// <returns></returns>
        public static string RemoveEquipment(CoreLimsContext dbContext, string EqId)
        {
            var eq = getEquipment(dbContext, EqId);
            if (eq.Status != "报废")
                return "只有报废的设备才能删除！";

            var eqinfo = getEqInfo(dbContext, EqId);
            var UsageRecords = getUsageRecords(dbContext, EqId);
            var Maintains = getMaintains(dbContext, EqId);
            var MaintainEvents = getMaintainEvents(dbContext, EqId);

            if (eq != null)
                dbContext.Equipment.Remove(eq);
            if (eqinfo != null)
                dbContext.EquipmentInfo.Remove(eqinfo);
            foreach (var item in UsageRecords)
            {
                dbContext.UsageRecord.Remove(item);
            }
            foreach (var item in Maintains)
            {
                dbContext.Maintain.Remove(item);
            }
            foreach (var item in MaintainEvents)
            {
                dbContext.MaintainEvent.Remove(item);
            }
            dbContext.SaveChanges();

            return "设备及相关信息删除成功！";
        }

        /// <summary>
        /// 删除指定的设备维护事件
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string RemoveMaintainEvent(CoreLimsContext dbContext, string Id)
        {
            var me = getMaintainEvent(dbContext, Id);
            var mlist = getMaintainsByEventId(dbContext, Id);

            if (me != null)
                dbContext.MaintainEvent.Remove(me);
            foreach (var item in mlist)
            {
                dbContext.Maintain.Remove(item);
            }
            dbContext.SaveChanges();

            return "设备维护事件及相关维护记录删除成功！";
        }

        public static string DoMaintainEvent(CoreLimsContext dbContext, EqMaintain maintain)
        {
            var me = Eq.getMaintainEvent(dbContext, maintain.MaintainEventId);
            me.LastTime = (DateTime)(maintain.EndTime is null ? DateTime.Now : maintain.EndTime);

            dbContext.Maintain.Add(maintain);
            dbContext.MaintainEvent.Update(me);
            dbContext.SaveChanges();

            return "操作成功！";
        }
    }
}
